#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QShortcut* shortcut = new QShortcut(QKeySequence(Qt::Key_Delete), ui->listWidget);
    connect(shortcut, SIGNAL(activated()), this, SLOT(deleteItem()));

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_action_Quit_triggered()
{
    qApp->exit(0);
}

void MainWindow::on_action_Open_triggered()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open ConfigFile"), "",
                                        tr("FileBackupConfig Files (*.fbc)"));
    if (fileName != "") {
        QFile* file = new QFile(fileName);
        if (!file->open(QIODevice::ReadOnly)) {
            QMessageBox::critical(this, tr("Error"),
                            tr("Could not open file"));
            return;
        }
        QXmlStreamReader xml(file);
        while(!xml.atEnd() &&
            !xml.hasError()) {
                QXmlStreamReader::TokenType token = xml.readNext();
                if(token == QXmlStreamReader::StartDocument) {
                    continue;
                }
                if(token == QXmlStreamReader::StartElement) {
                    if(xml.name() == "Config") {
                        continue;
                    }
                    if(xml.name() == "Sources") {
                        continue;
                    }
                    if(xml.name() == "Destination") {
                        ui->lineEdit->setText(xml.readElementText());
                        continue;
                    }
                    if(xml.name() == "Source") {
                        ui->listWidget->addItem(xml.readElementText());
                    }
                }
            }
            /* Error handling. */
            if(xml.hasError()) {
                QMessageBox::critical(this,
                                      tr("Error"),
                                      xml.errorString(),
                                      QMessageBox::Ok);
            }
            xml.clear();
        file->close();
    }
}

void MainWindow::on_action_Save_triggered()
{
    // check if the data is there and give some visible clues
    if ((ui->listWidget->count() == 0) || (ui->lineEdit->text() == ""))
    {
        if (ui->lineEdit->text() == "") ui->lineEdit->setStyleSheet("border: 2px solid red" );
        if (ui->listWidget->count() == 0) ui->listWidget->setStyleSheet("border: 2px solid red" );
        return;
    }

    QString fileName = QFileDialog::getSaveFileName(this, tr("Save ConfigFile"), "",
                                        tr("FileBackupConfig Files (*.fbc)"));
    if (!fileName.isEmpty()) {
        // check if possible
        QFile file( fileName );
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
             return;

        QXmlStreamWriter* xmlWriter = new QXmlStreamWriter();
        xmlWriter->setDevice(&file);
        xmlWriter->writeStartDocument();
        // Begin Config
        xmlWriter->writeStartElement("Config");

        // Begin Sources
        xmlWriter->writeStartElement("Sources");


        // fill it
        for(int row = 0; row < ui->listWidget->count(); row++)
        {
            QListWidgetItem *item = ui->listWidget->item(row);
            xmlWriter->writeStartElement("Source");
            xmlWriter->writeCharacters (item->text());
            xmlWriter->writeEndElement();
        }
        // end backup sources
        xmlWriter->writeEndElement();

        // add destination
        xmlWriter->writeStartElement("Destination");
        xmlWriter->writeCharacters (ui->lineEdit->text());
        xmlWriter->writeEndElement();

        // end config
         xmlWriter->writeEndElement();

        // End document
        xmlWriter->writeEndDocument();
        delete xmlWriter;
        file.close();
    }
}

void MainWindow::deleteItem()
{
    qDeleteAll(ui->listWidget->selectedItems());
}

void MainWindow::on_pushButton_clicked()
{
    ui->lineEdit->setStyleSheet("");
    QString fileName = QFileDialog::getExistingDirectory(this,tr("Select backup directory"),"/");
    ui->lineEdit->setText(fileName);
}

